<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        table {
            width: 500px;
            margin: 100px auto;
            border-collapse: collapse;
            text-align: center;
        }

        td,
        th {
            border: 1px solid #333;
        }

        thead tr {
            height: 40px;
            background-color: #ccc;
        }
    </style>
</head>

<body>

    <button>点击添加数据</button>
    <table cellspacing="0">
        <thead>
            <tr>
                <th>姓名</th>
                <th>科目</th>
                <th>成绩</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <!-- <tr>
                <td>魏璎珞</td>
                <td>JavaScript</td>
                <td>100</td>
                <td> <a href="javascript:;">删除</a></td>
            </tr> -->
        </tbody>
    </table>
    <script>
        // 1.先去准备好学生的数据
        var datas = [
            
        
        {
            name: '魏璎珞',
            subject: 'JavaScript',
            score: 100
        }, {
            name: '弘历',
            subject: 'JavaScript',
            score: 98
        }, {
            name: '傅恒',
            subject: 'JavaScript',
            score: 99
        }, {
            name: '明玉',
            subject: 'JavaScript',
            score: 88
        }, {
            name: '大猪蹄子',
            subject: 'JavaScript',
            score: 0
        },
        {
            name: '大胃王',
            subject: '吃饭',
            score: 200
        },
        {
            name: '大胃王222',
            subject: '吃饭',
            score: 200
        }];


        // 能够完成动态生成表格案例


        // <!-- <tr>
        //         <td>魏璎珞</td>
        //         <td>JavaScript</td>
        //         <td>100</td>
        //         <td> <a href="javascript:;">删除</a></td>
        //     </tr> -->


        //1 找人 
        //2 做事 
        //3 具体做什么事情 
        var  tbody=document.querySelector('tbody');
      function  showList() {
          //一上来先清空之前的内容
          tbody.innerHTML='';

     
        for(var i=0;i<datas.length;i++) {
            //1  根据数组的长度 datas 动态创建tr
            var tr=document.createElement('tr')
            var everyData=datas[i];
            // console.log(everyData)   //{name: "魏璎珞", subject: "JavaScript", score: 100}
            //2 根据 对象 动态创建td  
            for(var key in  everyData) {
                var td=document.createElement('td');
                //对象的值 everyData[key];
                td.innerText=everyData[key]
                // console.log(td)
                tr.appendChild(td);   
            }
            //手动创建    <td> <a href="javascript:;">删除</a></td> 
            var deletetd=document.createElement('td');
            var  a=document.createElement('a');
            a.href="javascript:;";
            a.innerText='删除'
            deletetd.appendChild(a);
            tr.appendChild(deletetd);
            a.onclick=function() {
                // alert(1)
                tbody.removeChild(this.parentNode.parentNode)

            }
           
            
            tbody.appendChild(tr)


        
         changeColor()  //调用隔行变色 函数
          



        }
      }

      showList()




       // //实现隔行变色 函数
        function  changeColor() {
            var   trs= tbody.children;
            for(var i=0;i<trs.length;i++) {
                if(i%2===0) {
                    trs[i].style.backgroundColor='pink'

                }else {
                    trs[i].style.backgroundColor='skyblue'; 
                }
            }
        }





        var  btn=document.querySelector('button');
        btn.onclick=function() {
            // alert('添加')
            // name: "魏璎珞", subject: "JavaScript", score: 100}
            //   例子 {name:'迪丽热巴',subject:'ui',score:86}
            var  obj={name:'迪丽热巴',subject:'ui',score:86};
            datas.push(obj);
            console.log(datas);

            showList()

        }


    </script>
</body>

</html>